package net.realsee.service.wcs.data.impl;

import cn.hutool.core.bean.BeanUtil;
import net.realsee.common.utils.StringUtils;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.framework.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import net.realsee.domain.wcs.data.bo.WcsDefectClassBo;
import net.realsee.domain.wcs.data.vo.WcsDefectClassVo;
import net.realsee.domain.wcs.data.WcsDefectClass;
import net.realsee.mapper.wcs.data.WcsDefectClassMapper;
import net.realsee.service.wcs.data.IWcsDefectClassService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 不良类别定义, 用于区分 系统报警代码，产品不良代码等Service业务层处理
 *
 * @author realsee
 * @date 2023-08-24
 */
@RequiredArgsConstructor
@Service
public class WcsDefectClassServiceImpl extends ServiceImpl<WcsDefectClassMapper, WcsDefectClass> implements IWcsDefectClassService {

    private final WcsDefectClassMapper baseMapper;

    /**
     * 查询不良类别定义
     */
    @Override
    public WcsDefectClassVo selectVoById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询不良类别定义列表
     */
    @Override
    public TableDataInfo<WcsDefectClassVo> selectVoPage(WcsDefectClassBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WcsDefectClass> lqw = buildVoQueryWrapper(bo);
        Page<WcsDefectClassVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询不良类别定义列表
     */
    @Override
    public List<WcsDefectClassVo> selectVoList(WcsDefectClassBo bo) {
        LambdaQueryWrapper<WcsDefectClass> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsDefectClass> buildVoQueryWrapper(WcsDefectClassBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsDefectClass> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, WcsDefectClass::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getDefectClass()), WcsDefectClass::getDefectClass, bo.getDefectClass());
        return lqw;
    }

    /**
     * 新增不良类别定义
     */
    @Override
    public Boolean insertByBo(WcsDefectClassBo bo) {
        WcsDefectClass add = BeanUtil.toBean(bo, WcsDefectClass.class);
        return this.save(add);
    }

    /**
     * 修改不良类别定义
     */
    @Override
    public Boolean updateByBo(WcsDefectClassBo bo) {
        WcsDefectClass update = BeanUtil.toBean(bo, WcsDefectClass.class);
        return this.updateById(update);
    }

    /**
     * 批量删除不良类别定义
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }
}
